package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.List;

import com.atguigu.gulimall.product.entity.ProductAttrValueEntity;
import com.atguigu.gulimall.product.entity.vo.AttrRespVo;
import com.atguigu.gulimall.product.entity.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 商品属性
 *
 * @author xuweixing
 * @email xuweixing@gmail.com
 * @date 2021-05-08 17:02:39
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * 列表
     */
//    @RequestMapping("/base/list")
    @GetMapping("/{type}/list/{catId}")
    public R baseList(@RequestParam Map<String, Object> params,
                      @PathVariable("catId") Long catId,
                      @PathVariable("type") String type){
        PageUtils page = attrService.queryPageBtCatId(params,catId,type);

        return R.ok().put("page", page);
    }

    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
//		AttrEntity attr = attrService.getById(attrId);
        AttrRespVo respVo = attrService.getAttrDetail(attrId);

        return R.ok().put("attr", respVo);
    }

    /**
     * 获取spu属性
     *
     */
    @RequestMapping("/base/listforspu/{spuId}")
    public R listforspu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> attrs = attrService.listforspu(spuId);

        return R.ok().put("data", attrs);
    }

    /**
     * 修改spu属性
     *
     */
    @RequestMapping("/update/{spuId}")
    public R updatespuattr(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> entitys){
        attrService.updatespuattr(spuId,entitys);

        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVO attr){
//		attrService.save(attr);

        attrService.saveAttr(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVO attr){
//		attrService.updateById(attr);
        attrService.updateAttrById(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
